package org.gitee.weapp.service.impl;

import org.gitee.weapp.domain.entity.AddressInfo;
import org.gitee.weapp.mapper.AddressInfoMapper;
import org.gitee.weapp.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressInfoMapper addressInfoMapper;

    @Override
    @Transactional
    public void addAddress(AddressInfo addressInfo) {
        if (addressInfo.getIsDefault() == Boolean.TRUE) {
            addressInfoMapper.updateDefault(addressInfo.getOpenId(), false);
        }
        addressInfoMapper.insert(addressInfo);
    }

    @Override
    @Transactional
    public void editAddress(AddressInfo addressInfo) {
        if (addressInfo.getIsDefault() == Boolean.TRUE) {
            addressInfoMapper.updateDefault(addressInfo.getOpenId(), false);
        }
        addressInfoMapper.update(addressInfo);
    }
}
